package com.four.peoplemanage.gift.service;

import com.four.peoplemanage.common.bean.Page;
import com.four.peoplemanage.gift.dao.GiftDao;
import com.four.peoplemanage.gift.entity.Gift;
import com.four.peoplemanage.overtime.entity.Overtime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class GiftService {
    @Autowired
    GiftDao giftDao;
    //添加礼品
    public boolean insert(Gift gift){
        if (gift == null){
            return false;
        }
        gift.setGift_claim_status("待领取");
        gift.setStatus("1");
        return giftDao.addGift(gift)>0;
    }

    //分页
    public Page<Gift> getPageGift (int pageNum, int pageSize, String giftname,String gift_claim_status, String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Gift> list=giftDao.getGift(start,pageSize,giftname,gift_claim_status);
        //获取总条目数
        int total=giftDao.countGift(giftname,gift_claim_status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Gift> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //分页查看全部
    public Page<Gift> getPageGift1 (int pageNum, int pageSize, String giftname,String gift_claim_status,String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Gift> list=giftDao.getGift1(start,pageSize,giftname,gift_claim_status);
        //获取总条目数
        int total=giftDao.countGift1(giftname,gift_claim_status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Gift> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //领取
    public  boolean updateStatus(Gift gift){
        if (gift==null){
            return false;
        }
        gift.setGift_claim_status("已领取");
        gift.setStatus("0");
        return giftDao.updateOvertimeById(gift)>0;
    }
}
